package com.sgb.kjwl.viewmodel.pick;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.PickClEntity;
import com.sgb.kjwl.model.entity.PickDeviceEntity;
import com.sgb.kjwl.model.entity.PickGcEntity;
import com.sgb.kjwl.model.entity.PickLaborEntity;
import com.sgb.kjwl.model.entity.PickListUploadEntity;
import com.sgb.kjwl.model.entity.PickProjectEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.PickProjectRepertory;
import com.sgb.kjwl.view.ui.activity.pick.PickProjectActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

@Deprecated
public class PickProjectModel extends BaseViewModel {

    private final PickProjectActivity activity;
    private final PickProjectRepertory repertory;
    private int gcPage = 1;
    private int clPage = 1;
    private int devicePage = 1;
    private int laborPage = 1;
    private int size = 20;
    private PickListUploadEntity entity;

    public PickProjectModel(PickProjectActivity pickProjectActivity, PickProjectRepertory pickProjectRepertory) {
        activity = pickProjectActivity;
        repertory = pickProjectRepertory;
        entity = new PickListUploadEntity();
    }

    /**
     * 接口请求中no和projectNo参数相同，但后台取用各异，遂都传相同参数，待修改
     *
     * @param no
     */
    public void getPickGcModel(String no, boolean loadMore) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(true);

        gcPage = loadMore ? ++gcPage : 1;

        entity.setItemId(no);
        entity.setPageSize(size);
        entity.setPageNum(gcPage);

        APIManager.getInstance().getPickAPI().getPickGcList(entity).enqueue(new Callback<BaseEntity<ListResponse<List<PickGcEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<PickGcEntity>>>> call, Response<BaseEntity<ListResponse<List<PickGcEntity>>>> response) {
                DialogHelper.dismissProgressDialog();

                if (response.body() == null || response.body().getState() == null) return;

                if (response.body().getData().getList().size() == 0) {
                    activity.loadNoData("GC_EMPTY");
                } else {
                    activity.showGc(response.body().getData().getList());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<PickGcEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MLog.d(AppConstant.API_LOG, t.getMessage());
                activity.loadNoData("GC_EMPTY");
            }
        });

//        repertory.getPickGc(no, no, size, gcPage).subscribe(new Consumer<BaseEntity<ListResponse<List<PickGcEntity>>>>() {
//            @Override
//            public void accept(BaseEntity<ListResponse<List<PickGcEntity>>> listResponseBaseEntity) throws Exception {
//                DialogHelper.dismissProgressDialog();
//                if (listResponseBaseEntity.getData().getList().size() == 0) {
//                    if (loadMore) {
//                        activity.loadNoData("GC_COMPLETE");
//                    } else {
//                        activity.loadNoData("GC_EMPTY");
//                    }
//                } else {
//                    activity.showGc(listResponseBaseEntity.getData().getList());
//                }
//            }
//        }, new Consumer<Throwable>() {
//            @Override
//            public void accept(Throwable throwable) throws Exception {
//                DialogHelper.dismissProgressDialog();
//                MLog.d(AppConstant.API_LOG, throwable.getMessage());
//                activity.loadNoData("GC_EMPTY");
//            }
//        });
    }


    public void getPickClModel(String no, boolean loadMore) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(true);
        clPage = loadMore ? ++clPage : 1;

        entity.setItemId(no);
        entity.setPageSize(size);
        entity.setPageNum(clPage);

        APIManager.getInstance().getPickAPI().getPickClList(entity).enqueue(new Callback<BaseEntity<ListResponse<List<PickClEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<PickClEntity>>>> call, Response<BaseEntity<ListResponse<List<PickClEntity>>>> response) {
                DialogHelper.dismissProgressDialog();

                if (response.body() == null || response.body().getState() == null) return;

                if (response.body().getData().getList().size() == 0) {
                    activity.loadNoData("CL_EMPTY");
                } else {
                    activity.showCl(response.body().getData().getList());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<PickClEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MLog.d(AppConstant.API_LOG, t.getMessage());
                activity.loadNoData("CL_EMPTY");
            }
        });

//        repertory.getPickCl(no, no, size, clPage).subscribe(new Consumer<BaseEntity<ListResponse<List<PickClEntity>>>>() {
//            @Override
//            public void accept(BaseEntity<ListResponse<List<PickClEntity>>> listResponseBaseEntity) throws Exception {
//                DialogHelper.dismissProgressDialog();
//                if (listResponseBaseEntity.getData().getList().size() == 0) {
//                    if (loadMore) {
//                        activity.loadNoData("CL_COMPLETE");
//                    } else {
//                        activity.loadNoData("CL_EMPTY");
//                    }
//                } else {
//                    activity.showCl(listResponseBaseEntity.getData().getList());
//                }
//            }
//        }, new Consumer<Throwable>() {
//            @Override
//            public void accept(Throwable throwable) throws Exception {
//                DialogHelper.dismissProgressDialog();
//                MLog.d(AppConstant.API_LOG, throwable.getMessage());
//                activity.loadNoData("CL_EMPTY");
//            }
//        });
    }

    public void getDeviceModel(String no, boolean loadMore) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(true);
        devicePage = loadMore ? ++devicePage : 1;

        entity.setItemId(no);
        entity.setPageSize(size);
        entity.setPageNum(devicePage);

        APIManager.getInstance().getPickAPI().getPickDeviceList(entity).enqueue(new Callback<BaseEntity<ListResponse<List<PickDeviceEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<PickDeviceEntity>>>> call, Response<BaseEntity<ListResponse<List<PickDeviceEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) return;

                if (response.body().getData().getList().size() == 0) {
                    activity.loadNoData("DEVICE_EMPTY");
                } else {
                    activity.showDevice(response.body().getData().getList());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<PickDeviceEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MLog.d(AppConstant.API_LOG, t.getMessage());
                activity.loadNoData("DEVICE_EMPTY");
            }
        });

//        repertory.getPickDevice(no, no, size, devicePage).subscribe(new Consumer<BaseEntity<ListResponse<List<PickDeviceEntity>>>>() {
//            @Override
//            public void accept(BaseEntity<ListResponse<List<PickDeviceEntity>>> listResponseBaseEntity) throws Exception {
//                DialogHelper.dismissProgressDialog();
//                if (listResponseBaseEntity.getData().getList().size() == 0) {
//                    if (loadMore) {
//                        activity.loadNoData("DEVICE_COMPLETE");
//                    } else {
//                        activity.loadNoData("DEVICE_EMPTY");
//                    }
//                } else {
//                    activity.showDevice(listResponseBaseEntity.getData().getList());
//                }
//            }
//        }, new Consumer<Throwable>() {
//            @Override
//            public void accept(Throwable throwable) throws Exception {
//                DialogHelper.dismissProgressDialog();
//                MLog.d(AppConstant.API_LOG, throwable.getMessage());
//                activity.loadNoData("DEVICE_EMPTY");
//            }
//        });
    }

    public void getLaborModel(String no, boolean loadMore) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(true);
        laborPage = loadMore ? ++laborPage : 1;

        entity.setItemId(no);
        entity.setPageSize(size);
        entity.setPageNum(laborPage);

        APIManager.getInstance().getPickAPI().getPickLaborList(entity).enqueue(new Callback<BaseEntity<ListResponse<List<PickLaborEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<PickLaborEntity>>>> call, Response<BaseEntity<ListResponse<List<PickLaborEntity>>>> response) {
                DialogHelper.dismissProgressDialog();

                if (response.body() == null || response.body().getState() == null) return;

                if (response.body().getData().getList().size() == 0) {
                    activity.loadNoData("LABOR_EMPTY");
                } else {
                    activity.showLabor(response.body().getData().getList());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<PickLaborEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MLog.d(AppConstant.API_LOG, t.getMessage());
                activity.loadNoData("LABOR_EMPTY");
            }
        });


//        repertory.getPickLabor(no, no, size, laborPage).subscribe(new Consumer<BaseEntity<ListResponse<List<PickLaborEntity>>>>() {
//            @Override
//            public void accept(BaseEntity<ListResponse<List<PickLaborEntity>>> listResponseBaseEntity) throws Exception {
//                DialogHelper.dismissProgressDialog();
//                if (listResponseBaseEntity.getData().getList().size() == 0) {
//                    if (loadMore) {
//                        activity.loadNoData("LABOR_COMPLETE");
//                    } else {
//                        activity.loadNoData("LABOR_EMPTY");
//                    }
//                } else {
//                    activity.showLabor(listResponseBaseEntity.getData().getList());
//                }
//            }
//        }, new Consumer<Throwable>() {
//            @Override
//            public void accept(Throwable throwable) throws Exception {
//                DialogHelper.dismissProgressDialog();
//                MLog.d(AppConstant.API_LOG, throwable.getMessage());
//                activity.loadNoData("LABOR_EMPTY");
//            }
//        });
    }

    public void getPickProject(String no) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(true);

        APIManager.getInstance().getPickAPI().getProjectDetail(no).enqueue(new Callback<BaseEntity<PickProjectEntity.ProjectBean>>() {
            @Override
            public void onResponse(Call<BaseEntity<PickProjectEntity.ProjectBean>> call, Response<BaseEntity<PickProjectEntity.ProjectBean>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) return;
                activity.showProjectDetail(response.body().getData());
            }

            @Override
            public void onFailure(Call<BaseEntity<PickProjectEntity.ProjectBean>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MLog.d(AppConstant.API_LOG, t.getMessage());
            }
        });

//        repertory.getPickProject(no).subscribe(new Consumer<BaseEntity<PickProjectEntity>>() {
//            @Override
//            public void accept(BaseEntity<PickProjectEntity> pickProjectEntityBaseEntity) throws Exception {
//                DialogHelper.dismissProgressDialog();
//                activity.showProjectDetail(pickProjectEntityBaseEntity.getData());
//            }
//        }, new Consumer<Throwable>() {
//            @Override
//            public void accept(Throwable throwable) throws Exception {
//                DialogHelper.dismissProgressDialog();
//                MLog.d(AppConstant.API_LOG, throwable.getMessage());
//            }
//        });
    }

}
